﻿#region References

using System.Collections.Generic;
using System.Windows.Input;
using Dev_house_Project.ViewModel.DataService;

#endregion

namespace Dev_house_Project.ViewModel
{
    public class MainViewModel:ViewModelBase
    {
        #region Fields
        
        private readonly DataServiceClient _dataClient;
        private DataStorageTypeEnum _dataStorageType;
        private List<Company> _companiesList;

        private DelegateCommand _saveData;

        #endregion

        #region Constructor

        public MainViewModel(DataStorageTypeEnum dataStorageType)
        {
            _dataClient = new DataServiceClient();
            
            _dataClient.GetDataFromLocalStorageCompleted += dataClient_GetDataFromLocalStorageCompleted;
            _dataClient.GetDataFromRemoteStorageCompleted += dataClient_GetDataFromRemoteStorageCompleted;
            _dataClient.SynchronizeCompleted += _dataClient_SynchronizeCompleted;

            _companiesList = new List<Company>();

            DataStorageType = dataStorageType;
        }       

        public MainViewModel():this(DataStorageTypeEnum.RemoteStorage){ }

        #endregion

        #region Properties

        public List<Company> Companies
        {
            get
            {
                return _companiesList;
            }
            private set
            {
                _companiesList = value;
                OnPropertyChanged("Companies");
            }
        }

        public DataStorageTypeEnum DataStorageType
        {
            get
            {
                return _dataStorageType;
            }
            set
            {
                _dataStorageType = value;
                _dataClient.SynchronizeAsync();
                OnPropertyChanged("DataStorageType");
            }
        }

        #endregion

        #region Commands

        public ICommand SaveDataCommand
        {
            get
            {
                if (null == _saveData)
                {
                    _saveData = new DelegateCommand(SaveData);
                }
                return _saveData;
            }
        }

        #endregion

        #region Methods

        void SaveData()
        {
            if (DataStorageType ==  DataStorageTypeEnum.LocalStorage)
            {
                _dataClient.SubmitChangesToLocalStorageAsync(Companies);
            }
            else
            {
                _dataClient.SubmitChangesToRemoteStorageAsync(Companies);
            }
        }

        #endregion

        #region EventHandlers

        void dataClient_GetDataFromRemoteStorageCompleted(object sender, GetDataFromRemoteStorageCompletedEventArgs e)
        {
            Companies = e.Result;
        }

        void dataClient_GetDataFromLocalStorageCompleted(object sender, GetDataFromLocalStorageCompletedEventArgs e)
        {
            Companies = e.Result;
        }

        void _dataClient_SynchronizeCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            if (_dataStorageType == DataStorageTypeEnum.LocalStorage)
            {
                _dataClient.GetDataFromLocalStorageAsync();
            }
            else
            {
                _dataClient.GetDataFromRemoteStorageAsync();
            }
        }

        #endregion
    }

    public enum DataStorageTypeEnum
    {
        LocalStorage,
        RemoteStorage
    }
        
}
